﻿// Models/MultiRoundMeasurementData.cs
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;

namespace MultiDAQ_Analysis.Models
{
    /// <summary>
    /// 多轮测量数据存储模型
    /// </summary>
    /// <summary>
    /// 多轮测量数据存储模型
    /// </summary>
    public class MultiRoundMeasurementData
    {
        [JsonProperty("measurementId")]
        public string MeasurementId { get; set; } = Guid.NewGuid().ToString();

        [JsonProperty("measurementType")]
        public string MeasurementType { get; set; }

        [JsonProperty("startTime")]
        public DateTime StartTime { get; set; }

        [JsonProperty("endTime")]
        public DateTime EndTime { get; set; }

        [JsonProperty("totalRounds")]
        public int TotalRounds { get; set; }

        [JsonProperty("measurementTimeMinutes")]
        public int MeasurementTimeMinutes { get; set; }

        [JsonProperty("channelResults")]
        public Dictionary<int, ChannelMultiRoundResult> ChannelResults { get; set; } = new Dictionary<int, ChannelMultiRoundResult>();

        [JsonProperty("parameters")]
        public object Parameters { get; set; }

        // 新增：计算上下文信息
        [JsonProperty("calculationContext")]
        public MeasurementCalculationContextData CalculationContext { get; set; }
    }

    /// <summary>
    /// 单轮测量结果
    /// </summary>
    public class RoundResult
    {
        [JsonProperty("roundNumber")]
        public int RoundNumber { get; set; }

        [JsonProperty("alphaCountRate")]
        public double AlphaCountRate { get; set; }

        [JsonProperty("betaCountRate")]
        public double BetaCountRate { get; set; }

        [JsonProperty("alphaResult")]
        public double AlphaResult { get; set; }

        [JsonProperty("betaResult")]
        public double BetaResult { get; set; }

        [JsonProperty("timestamp")]
        public DateTime Timestamp { get; set; }

        // 新增：统计点数信息
        [JsonProperty("alphaTotalCounts")]
        public long AlphaTotalCounts { get; set; }

        [JsonProperty("betaTotalCounts")]
        public long BetaTotalCounts { get; set; }

        [JsonProperty("measurementDurationSeconds")]
        public int MeasurementDurationSeconds { get; set; } 

        // 添加缺失的属性
        [JsonProperty("deductedTotalCounts")]
        public long DeductedTotalCounts { get; set; }
    }
    /// <summary>
    /// 计算上下文数据（用于JSON序列化）
    /// </summary>
    public class MeasurementCalculationContextData
    {
        [JsonProperty("totalRounds")]
        public int TotalRounds { get; set; }

        [JsonProperty("measurementTimeMinutes")]
        public int MeasurementTimeMinutes { get; set; }

        [JsonProperty("calculatorType")]
        public string CalculatorType { get; set; }

        [JsonProperty("parameters")]
        public object Parameters { get; set; }

        [JsonProperty("selectedChannels")]
        public List<int> SelectedChannels { get; set; } = new List<int>();

        [JsonProperty("contextCreatedTime")]
        public DateTime ContextCreatedTime { get; set; }
    }

    /// <summary>
    /// 单个通道的多轮结果
    /// </summary>
    public class ChannelMultiRoundResult
    {
        [JsonProperty("channelIndex")]
        public int ChannelIndex { get; set; }

        [JsonProperty("roundResults")]
        public List<RoundResult> RoundResults { get; set; } = new List<RoundResult>();

        [JsonProperty("averageResult")]
        public RoundResult AverageResult { get; set; }

        [JsonProperty("statistics")]
        public MeasurementStatistics Statistics { get; set; }
    }


    /// <summary>
    /// 测量统计信息
    /// </summary>
    public class MeasurementStatistics
    {
        [JsonProperty("alphaStandardDeviation")]
        public double AlphaStandardDeviation { get; set; }

        [JsonProperty("betaStandardDeviation")]
        public double BetaStandardDeviation { get; set; }

        [JsonProperty("alphaRelativeError")]
        public double AlphaRelativeError { get; set; }

        [JsonProperty("betaRelativeError")]
        public double BetaRelativeError { get; set; }

        [JsonProperty("alphaConfidenceInterval")]
        public (double Lower, double Upper) AlphaConfidenceInterval { get; set; }

        [JsonProperty("betaConfidenceInterval")]
        public (double Lower, double Upper) BetaConfidenceInterval { get; set; }
    }
}